<template>
	<div class="user-page">
		<!--查询表单-->
		<el-card class="box-card mb10" shadow="never">
			<el-form ref="searchFormRef" class="search-form" :inline="true" label-position="left" label-width="75px" :model="searchForm">
				<el-row :gutter="30" justify="start">
					<el-col :span="6">
						<el-form-item class="form-item" label="报告名称:" prop="reportName">
							<el-input v-model="searchForm.reportName" placeholder="请输入报告名称" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item class="form-item" label="报告类型:" prop="reportType">
							<dict-select v-model="searchForm.reportType" :dictKey="'report_type'" placeholder="请选择报告类型" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item class="form-item" label="报告状态:" prop="reportStatus">
							<el-select v-model="searchForm.reportStatus" class="el-form-item" clearable placeholder="请选择报告状态">
								<el-option label="正常" value="0" />
								<el-option label="停用" value="1" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<div class="search-btn">
							<el-button type="primary" @click="handleSearch">查询</el-button>
							<el-button @click="handleReset">重置</el-button>
						</div>
					</el-col>
				</el-row>
			</el-form>
		</el-card>
		<el-card class="box-card" shadow="never">
			<static-table :tableData="tableData" :loading="loading" :seqConfig="{ startIndex: (pageNum - 1) * pageSize }">
				<template #vux-search>
					<el-row :gutter="12" class="mb10">
						<el-button class="ml8" type="primary" v-has="'system:report:add'" @click="handleAdd">
							<el-icon><ele-Plus /></el-icon>
							&nbsp;新增
						</el-button>
					</el-row>
				</template>
				<template #vux-column>
					<vxe-column type="seq" title="序号" width="60" />
					<vxe-column field="reportName" min-width="150" show-overflow="tooltip" title="报告名称" />
					<vxe-column field="reportType" min-width="120" :formatter="formatterType" show-overflow="tooltip" title="报告类型" />
					<vxe-column field="reportTime" min-width="150" show-overflow="tooltip" title="报告时间" />
					<vxe-column field="reportStatus" min-width="120" title="报告状态">
						<template #default="{ row }">
							<el-tag effect="plain" type="danger" v-if="row.reportStatus == 1">停用</el-tag>
							<el-tag effect="plain" type="success" v-if="row.reportStatus == 0">正常</el-tag>
						</template>
					</vxe-column>
					<vxe-column field="createBy" min-width="120" title="操作人" />
					<vxe-column field="createTime" min-width="150" title="创建时间" />
					<vxe-column field="operation" fixed="right" min-width="120" title="操作">
						<template #default="{ row }">
							<div class="vxe-action">
								<a @click="handleUpdate(row)" v-has="'system:report:update'">修改</a>
								<a @click="handleDelete(row)" v-has="'system:report:delete'">删除</a>
								<a @click="handleDownLoad(row)" v-has="'system:report:down'">文件下载</a>
							</div>
						</template>
					</vxe-column>
				</template>
			</static-table>
			<el-pagination background :current-page="pageNum" :layout="layout" :page-size="pageSize" :total="dataCount" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
		</el-card>
		<EditReportModal ref="editModalRef" @peak-modal-ok="handleModalOk" />
	</div>
</template>
<script name="ReportManage" setup>
import global from '@/utils/global'
import useTableList from '@/hooks/useTableList'
import useModal from '@/hooks/useModal'
import fileDownload from 'js-file-download'
const EditReportModal = defineAsyncComponent(() => import('./leafPages/EditReportModal.vue'))

/*
 *@Description: 状态初始化
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-08-28 10:11:35
 */

const state = reactive({
	searchForm: {
		reportName: '',
		reportType: '',
		reportStatus: '',
	},
	createTimeAry: [],
	searchFormRef: null,
})
const { searchForm, searchFormRef } = toRefs(state)

const queryUrl = '/meta-system/web/v1/sysReport/list'
const { loading, dataCount, handleInitQuery, getDictKeyText, handleModalOk, tableData, pageNum, pageSize, layout, handleSizeChange, handleCurrentChange } = useTableList(queryUrl, state.searchForm)

const { editModalRef, handleAdd, handleUpdate } = useModal()

/*
 *@Description: 方法模块
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-08-28 10:16:03
 */
const handleSearch = () => {
	handleInitQuery()
}
const handleReset = () => {
	state.searchFormRef.resetFields()
	state.createTimeAry = []
	handleInitQuery()
}
const handleDelete = row => {
	global.$baseConfirm(
		row ? `是否确认删除名称为"--【${row.reportName}】--"的数据?` : '是否确认删除当前选中的数据？',
		'提示',
		() => {
			global.$fetchPostData(`/meta-system/web/v1/sysReport/deleteReportById`, { idStr: row.id }).then(data => {
				if (data.code == 200) {
					global.$baseMessage(data.msg, 'success')
					handleModalOk(2)
				} else {
					global.$baseMessage(data.msg, 'error')
				}
			})
		},
		() => {}
	)
}
const formatterType = ({ cellValue }) => {
	return getDictKeyText('report_type', cellValue)
}
const handleDownLoad = row => {
	global.$fetchGetDownFile('/meta-system/web/v1/upload/downloadByFileId?fileId=' + row.fileId).then(data => {
		fileDownload(data, row.fileName)
	})
}
</script>
<style lang="scss" scoped></style>
